package com.smartwebx.ha.dal.util.core;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.time.DateUtils;

import com.google.common.collect.Lists;
import com.smartwebx.ha.dal.inter.water.IFeatureFlowDao;
import com.smartwebx.ha.dal.inter.water.IFeatureWaterDao;
import com.smartwebx.ha.dal.usebean.DateValue;
import com.smartwebx.ha.dal.usebean.FeatureHighLow;
import com.smartwebx.ha.dal.usebean.FeatureWaterPeriod;
import com.smartwebx.ha.dal.usebean.SplitDateAndTimePointBean;
import com.smartwebx.ha.dal.usebean.SplitDatePointBean;
import com.smartwebx.ha.dal.util.DateFullProcess;
import com.smartwebx.ha.dal.util.SplitDateAndTimePoint;
import com.smartwebx.ha.dal.util.SplitDatePoint;

/**
 * 特征库水情方法
 * 
 * @author zhangxi
 * 
 */
public class FeatureWaterFlowUtil {

	/**
	 * 单站同期水位对比
	 * 
	 * @param featureWater
	 *            特征库水情接口
	 * @param stcd
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	public static List<DateValue> getPeriodContrast(
			IFeatureWaterDao featureWater, String stcd, Date startDate,
			Date endDate) {
		SplitDatePointBean splitDatePoint = SplitDatePoint.splitDatePoint(
				startDate, endDate);
		String operatType = splitDatePoint.getOperatType();
		Date startTime = splitDatePoint.getStartDate();
		Date endTime = splitDatePoint.getEndDate();
		Date startHour = splitDatePoint.getStartHour();
		Date endHour = splitDatePoint.getEndHour();

		Date startD = null, endD = null;

		if ("127".indexOf(operatType) > -1) { // 开始日期为开始小时，结束日期为结束小时
			startD = startHour;
			endD = endHour;
		} else if ("36".indexOf(operatType) > -1) {// 开始日期为开始日期，结束日期为结束日期
			startD = startTime;
			endD = endTime;
		} else if ("45".indexOf(operatType) > -1) {// 开始日期为开始日期，结束日期为结束小时
			startD = startTime;
			endD = endHour;
		}

		if ("2357".indexOf(operatType) > -1) {
			endD = DateUtils.addDays(endD, -1);
		}

		Calendar startCal = DateUtils.toCalendar(startD);
		Calendar endCal = DateUtils.toCalendar(endD);
		int year = startCal.get(Calendar.YEAR);
		int startMonth = startCal.get(Calendar.MONTH) + 1;
		int startDay = startCal.get(Calendar.DAY_OF_MONTH);
		int endMonth = endCal.get(Calendar.MONTH) + 1;
		int endDay = endCal.get(Calendar.DAY_OF_MONTH);

		List<FeatureWaterPeriod> waterPeriods = featureWater
				.getPeriodAvgContrastWater(stcd, startMonth, endMonth,
						startDay, endDay);
		
		List<DateValue> process = Lists.newArrayList();
		Map<Date, Double> timePointMap = DateFullProcess.returnDateSectStr(
				startD, endD,0);
		for (FeatureWaterPeriod waterPeriod : waterPeriods) {
			
			GregorianCalendar greCal = new GregorianCalendar(year,
					waterPeriod.getMnth()-1, waterPeriod.getDay());

			Date time = greCal.getTime();
			if (timePointMap.containsKey(time)) {
				timePointMap.put(time, waterPeriod.getAvgWater());
			}
		}

		for (Date time : timePointMap.keySet()) {
			DateValue dv = new DateValue(time, timePointMap.get(time));
			process.add(dv);
		}
		return process;
	}

	/**
	 * 多年单站最高最低水位信息
	 * 
	 * @param featureWater
	 * @param stcd
	 * @return
	 */
	public static FeatureHighLow getMoreYearStatHighLowWater(IFeatureWaterDao featureWater,
			String stcd) {
		List<FeatureHighLow> fRiverHLList = featureWater
				.getMoreYearStatHighLowWater(stcd);

		FeatureHighLow fRiverHL = new FeatureHighLow();
		
		// 最大最小值比较
		if(!fRiverHLList.isEmpty()){
			// 最高水位最高值
			FeatureHighLow maxTopWater = FeatureHighLow.maxByOrder.max(fRiverHLList);
			// 最低水位最低值
			FeatureHighLow minDownWater = FeatureHighLow.minByOrder.min(fRiverHLList);
			
			fRiverHL.setMaxz(maxTopWater.getMaxz());
			fRiverHL.setMaxzTm(maxTopWater.getMaxzTm());
			fRiverHL.setMinz(minDownWater.getMinz());
			fRiverHL.setMinzTm(minDownWater.getMinzTm());
		}
		return fRiverHL;
	}
	
	/**
	 * 多年单站最高最低水位信息
	 * 
	 * @param featureFlow
	 * @param stcd
	 * @return
	 */
	public static FeatureHighLow getMoreYearStatHighLowFlow(IFeatureFlowDao featureFlow,
			String stcd) {
		List<FeatureHighLow> fRiverHLList = featureFlow
				.getMoreYearStatHighLowFlow(stcd);

		FeatureHighLow fRiverHL = new FeatureHighLow();

		// 最大最小值比较
		if (!fRiverHLList.isEmpty()) {
			FeatureHighLow fRiverMax = FeatureHighLow.maxByOrder
					.max(fRiverHLList);

			FeatureHighLow fRiverMin = FeatureHighLow.minByOrder
					.min(fRiverHLList);

			fRiverHL.setMaxz(fRiverMax.getMaxz());
			fRiverHL.setMaxzTm(fRiverMax.getMaxzTm());
			fRiverHL.setMinz(fRiverMin.getMinz());
			fRiverHL.setMinzTm(fRiverMin.getMinzTm());
		}
		return fRiverHL;
	}

}
